package com.ibeeking.found.file.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.file.rest.service.ISourceService;
import com.ibeeking.found.file.service.common.param.SourceAddParam;
import com.ibeeking.found.file.service.common.param.SourceModifyBatch;
import com.ibeeking.found.file.service.common.param.SourceParam;
import com.ibeeking.found.file.service.common.query.SourcePageQuery;
import com.ibeeking.found.file.service.common.query.SourceQuery;
import com.ibeeking.found.file.service.common.vo.SourceVO;
import com.ibeeking.found.file.service.common.vo.SourceViewVo;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName GoodsSourceController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-02-05 16:17
 **/
@RestController
@RequestMapping("/source/v1")
public class SourceController {

    @Resource
    private ISourceService goodsSourceServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询素材")
    @PostMapping(value = "/list")
    public List<SourceVO> list(@RequestBody @Validated(BaseValidationGroup.Update.class) SourceQuery sourceQuery) {
        return goodsSourceServiceImpl.list(sourceQuery);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询素材")
    @PostMapping(value = "/pageList")
    public Page<SourceVO> pageList(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) SourcePageQuery sourcePageQuery) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            sourcePageQuery.setShopId(currentUser.getCurrentShop().getShopId());
            sourcePageQuery.setMerchantsBusiness(1);
        }else {
            sourcePageQuery.setMerchantsBusiness(2);
        }
        return goodsSourceServiceImpl.pageList(sourcePageQuery);
    }

        @Log(logType = OperateLogTypeEnum.SAVE, describe = "添加素材")
	@PostMapping(value = "/add")
	public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) SourceAddParam sourceAddParam) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            sourceAddParam.setShopId(currentUser.getCurrentShop().getShopId());
            sourceAddParam.setMerchantsBusiness(1);
        }else {
            sourceAddParam.setShopId(GlobalConstant.SHOP_ID_ZERO);
            sourceAddParam.setMerchantsBusiness(2);
        }
        sourceAddParam.setCreateBy(currentUser.getId());
        sourceAddParam.setModifyBy(currentUser.getId());
		return goodsSourceServiceImpl.add(sourceAddParam);
	}

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改素材")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) SourceParam sourceParam) {
        sourceParam.setModifyBy(currentUser.getId());
        return goodsSourceServiceImpl.modify(sourceParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询素材详情")
    @GetMapping(value = "/view/{id}")
    public SourceViewVo view(@PathVariable("id") Long id) {
        return goodsSourceServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除素材")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return goodsSourceServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "批量删除素材")
    @PostMapping(value = "/delBatch")
    public Boolean delBatchIds(@RequestBody SourceModifyBatch sourceModifyBatch) {
        return goodsSourceServiceImpl.delBatchIds(sourceModifyBatch.getIds());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "移动到其它分组")
    @PostMapping(value = "/modifyBatch")
    public Boolean modifyBatchIds(@RequestBody @Validated(BaseValidationGroup.Insert.class) SourceModifyBatch sourceModifyBatch) {
        return goodsSourceServiceImpl.modifyBatchIds(sourceModifyBatch);
    }
}
